package com.futureCloudMOM.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.futureCloudMOM.common.exception.base.BaseException;
import com.futureCloudMOM.core.base.service.impl.BaseServiceImpl;
import com.futureCloudMOM.core.domain.BasicRoutingMaterial;
import com.futureCloudMOM.core.domain.BasicRoutingProcess;
import com.futureCloudMOM.core.domain.ProduceTaskBillRoutingMaterial;
import com.futureCloudMOM.core.mapper.ProduceTaskBillRoutingMaterialMapper;
import com.futureCloudMOM.core.service.BasicRoutingMaterialService;
import com.futureCloudMOM.core.service.ProduceTaskBillRoutingMaterialService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 【Produce_TaskBillRoutingMaterial】的数据库操作Service实现
 *
 * @author Administrator
 * @since 2023-06-06 16:03:09
 */
@Service
@AllArgsConstructor
public class ProduceTaskBillRoutingMaterialServiceImpl extends BaseServiceImpl<ProduceTaskBillRoutingMaterialMapper, ProduceTaskBillRoutingMaterial> implements ProduceTaskBillRoutingMaterialService {

    private final BasicRoutingMaterialService basicRoutingMaterialService;

    @Override
    public List<ProduceTaskBillRoutingMaterial> selectListByProcessId(Long processId) {
        return list(new LambdaQueryWrapper<ProduceTaskBillRoutingMaterial>()
                .eq(ProduceTaskBillRoutingMaterial::getTaskBillRoutingProcessId, processId));
    }

    @Override
    public void create(Long billProcessId, BasicRoutingProcess basicRoutingProcess) {
        List<BasicRoutingMaterial> routingMaterials = basicRoutingMaterialService.selectListByProcessId(basicRoutingProcess.getId());
        if (CollectionUtils.isEmpty(routingMaterials)) {
            throw new BaseException("序号为：" + basicRoutingProcess.getSubNo() + "的工序，用料信息未维护");
        }
        /*
        保存订单工序上料参数
        */
        routingMaterials.forEach(basicRoutingMaterial -> {
            ProduceTaskBillRoutingMaterial taskBillRoutingMaterial = ProduceTaskBillRoutingMaterial.getInstance(basicRoutingMaterial);
            taskBillRoutingMaterial.setTaskBillRoutingProcessId(billProcessId);
            save(taskBillRoutingMaterial);
        });
    }

    @Override
    public void deleteByProcessId(Long processId) {
        getBaseMapper().delete(new LambdaQueryWrapper<ProduceTaskBillRoutingMaterial>()
                .eq(ProduceTaskBillRoutingMaterial::getTaskBillRoutingProcessId, processId));
    }
}
